//
//  NDCalRateEnumView.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/11/4.
//

import SwiftUI

import SwiftUI

struct NDCalRateEnumView<T: Hashable & CustomStringConvertible>: View {
    let title: String
    let dataSource: [T]
    var onSelectionChanged: ((Int, T?) -> Void)?
    var onConfirmAction: ((T?) -> Void)?
    var onCloseAction: (() -> Void)?
    
    @State private var selectedItem: T?
    
    var body: some View {
        VStack(spacing: 0) {
            
            Spacer()
                .frame(height: 25)
            
            ZStack(alignment: .trailing) {
                titleView
                closeButton
            }

            ScrollView {
                LazyVStack(spacing: 10) {
                    ForEach(dataSource, id: \.self) { item in
                        rowView(for: item)
                    }
                }
            }
            .padding(.top, 15)
            
            Spacer()
            
            confirmButton
        }
    }
    
    private var titleView: some View {
        Text(title)
            .font(.system(size: 18, weight: .medium))
            .frame(maxWidth: .infinity, alignment: .center)
            .padding(.bottom, 16)
    }
    
    private func rowView(for item: T) -> some View {
        HStack {
            Spacer()
            Text(item.description)
                .frame(maxWidth: .infinity, alignment: .leading)
                .frame(height: 50)
                .font(.system(size: 16, weight: .semibold))
                .foregroundColor(item == selectedItem ? Color(hex: "#FFFFFF") : Color(hex: "#2D4173"))
                .padding(.horizontal, 16)
                .padding(.vertical, 5)
                .background(
                    RoundedRectangle(cornerRadius: 8)
                        .fill(item == selectedItem ? Color(hex: "#FD632B") : Color(hex: "#FFF6F3"))
                )
                .contentShape(Rectangle())
            
            Spacer()
        }
        .padding(.horizontal, 16)
        .onTapGesture {
            selectedItem = item
            let index = dataSource.firstIndex { $0 == selectedItem } ?? -1
            onSelectionChanged?(index, selectedItem)
        }
    }
    
    private var confirmButton: some View {
        Button(action: {
            onConfirmAction?(selectedItem)
        }) {
            Text("Confirm")
                .frame(maxWidth: .infinity)
                .font(.system(size: 18, weight: .bold))
                .foregroundColor(Color(hex: "#FFFFFF"))
                .padding(.horizontal, 15)
                .padding(.vertical, 10)
                .frame(height: 48)
                .background(Color(hex: "#271F24"))
                .clipShape(Capsule())
                .contentShape(Rectangle())
        }
        .padding(.horizontal, 16)
        .padding(.bottom, 16)
    }
    
    private var closeButton: some View {
        Button(action: {
            onCloseAction?()
        }) {
            Image(systemName: "xmark")
                .font(.system(size: 16))
                .frame(width: 20, height: 20)
                .foregroundColor(Color(hex: "#271F24"))
        }
        .padding(.top, -7)
        .padding(.trailing, 20)
    }
}

//// 使用示例
//struct ContentView: View {
//    @State private var showStringPicker = false
//    @State private var showIntPicker = false
//    @State private var showFloatPicker = false
//    
//    var body: some View {
//        VStack(spacing: 20) {
//            Button("选择字符串") {
//                showStringPicker = true
//            }
//            
//            Button("选择整数") {
//                showIntPicker = true
//            }
//            
//            Button("选择浮点数") {
//                showFloatPicker = true
//            }
//        }
//        .sheet(isPresented: $showStringPicker) {
//            NDCalRateEnumView(
//                title: "选择选项",
//                dataSource: ["选项A", "选项B", "选项C"],
//                onSelectionChanged: { index, item in
//                    print("选择了第\(index)项: \(item ?? "无")")
//                },
//                onConfirmAction: { item in
//                    print("确认选择: \(item ?? "无")")
//                    showStringPicker = false
//                },
//                onCloseAction: {
//                    showStringPicker = false
//                }
//            )
//        }
//        .sheet(isPresented: $showIntPicker) {
//            NDCalRateEnumView(
//                title: "选择数字",
//                dataSource: [1, 2, 3, 4, 5],
//                onSelectionChanged: { index, item in
//                    print("选择了第\(index)项: \(item ?? -1)")
//                },
//                onConfirmAction: { item in
//                    print("确认选择: \(item ?? -1)")
//                    showIntPicker = false
//                },
//                onCloseAction: {
//                    showIntPicker = false
//                }
//            )
//        }
//        .sheet(isPresented: $showFloatPicker) {
//            NDCalRateEnumView(
//                title: "选择数值",
//                dataSource: [1.5, 2.5, 3.5, 4.5],
//                onSelectionChanged: { index, item in
//                    print("选择了第\(index)项: \(item ?? 0.0)")
//                },
//                onConfirmAction: { item in
//                    print("确认选择: \(item ?? 0.0)")
//                    showFloatPicker = false
//                },
//                onCloseAction: {
//                    showFloatPicker = false
//                }
//            )
//        }
//    }
//}
